class ErrCode:
    OK = 0  # 请求成功
    UNKNOWN = -1  # 未知错误
    PARAMETER_ERROR = 400  # 【开发】缺少必填参数、参数类型错误、参数值错误
    INPUT_ERROR = 401  # 【用户】参数输入错误，通常由于用户输入错误等用户行为产生的错误
    NOT_AUTHORIZED = 403  # 【用户】未登录、登录过期
    NOT_FOUND = 404  # 【用户、开发】找不到对象
    METHOD_ERROR = 405  # 【开发】请求方式错误，如：不支持POST请求等
    NOT_PERMISSIONS = 420  # 【用户】权限不足、没有权限操作
    CONDITION_NOT_MET = 421  # 【用户】条件不满足
    SERVER_ERROR = 500  # 【开发】服务端错误
    SERVER_NOT_AVAILABLE = 502  # 【开发】服务不可用
    DATA_ERROR = 600  # 【开发】数据/数据库错误

    # 常用msg
    MSG_OK = 'ok'
    MSG_NOT_AUTHORIZED = '未登录，请先登录'
    MSG_NOT_FOUND = '找不到对象'
    MSG_NOT_PERMISSIONS = '您没有权限操作'
    MSG_SERVER_ERROR = '服务器错误、请联系管理员处理'

    code = OK  # 错误码
    msg = MSG_OK  # 错误信息

    def __init__(self, err_code, msg=None):
        self.code = err_code
        if msg:
            self.msg = msg
        else:
            self.get_msg()

    def get_msg(self, code=None):
        """ 通过code获取msg """
        code = code or self.code
        msg = None
        if code == self.OK:
            msg = self.MSG_OK
        elif code == self.NOT_AUTHORIZED:
            msg = self.MSG_NOT_AUTHORIZED
        elif code == self.NOT_FOUND:
            msg = self.MSG_NOT_FOUND
        elif code == self.NOT_PERMISSIONS:
            msg = self.MSG_NOT_PERMISSIONS
        elif code == self.SERVER_ERROR:
            msg = self.MSG_SERVER_ERROR
        return msg


class OK:
    err_code = ErrCode(ErrCode.OK)
    code = err_code.code
    msg = err_code.msg


class RRError(Exception):
    """ 请求响应错误 """
    def __init__(self, *args, data=None, **kwargs):
        if data is None:
            data = {}
        self.data = data
        super(RRError, self).__init__(*args, **kwargs)


class RequiredParametersError(RRError):
    """ 必填参数错误 """
    def __init__(self, messages=None, data=None, *args, **kwargs):
        super(RequiredParametersError, self).__init__(messages, *args, data=data, **kwargs)


class NotFound(RRError):
    """ 找不到对象 """
    pass
